package com.brkj.course;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.main.R;
import com.brkj.model.DS_Course;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseCoreActivity;
import com.brkj.util.view.PullListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dgl on 2014/4/25.
 */
public class CourseViewPush {

    private PullListView courseListView;
    private TextView noContent;

    private View view;
    private Context mContext;

    private CourseAdapter courseAdapter;
    List<DS_Course> courseList = new ArrayList<DS_Course>();
    private boolean hasShown = false;

    public CourseViewPush(Context context){
        mContext = context;
    }

    public View getView(){
        view =  LayoutInflater.from(mContext).inflate(R.layout.course_common_view, null);

        courseListView = (PullListView)view.findViewById(R.id.courseListView);
        noContent = (TextView)view.findViewById(R.id.noContent);

        courseListView.hideFooterView();

        return view;
    }

    public void show(){
        if((hasShown)&& (courseList.size()>0)){
            return;
        }else{
            getCourse();
            hasShown = true;
        }
        getCourse();
    }

    private void getCourse() {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetPushCourse.params.UserID, MyApplication.myUserID);

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetPushCourse.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)mContext) {


            @Override
            public void onFailure(Throwable t, String strMsg) {
                // TODO Auto-generated method stub
                super.onFailure(t, strMsg);
                courseListView.onRefreshComplete();
            }

            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

//				HIF_GetCourse.result result = JsonUtil.fromJson((String)t, HIF_GetCourse.result.class);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_Course>>(){}.getType();
                courseList = gson.fromJson((String)t, listType);



                if((courseList != null) && (courseList.size() > 0)){
                    courseAdapter = new CourseAdapter(mContext, courseList);
                    courseListView.setAdapter(courseAdapter);

                    courseListView.setVisibility(View.VISIBLE);
                    noContent.setVisibility(View.GONE);

                }else{
                    courseListView.setVisibility(View.GONE);
                    noContent.setVisibility(View.VISIBLE);
                    noContent.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getCourse();
                        }
                    });
                    ((BaseCoreActivity)mContext).showToast("未获取到课程！");
                }

            }

        });

    }

}
